﻿import sys
import codecs


def check_encoding(filename, codec, lines):
    fl = codecs.open(filename, encoding=codec)
    try:
        if lines:
            fl.readlines(lines)
        else:
            fl.read()
    except ValueError:
        return False
    return True


def detect_encoding(filename, lines):
    if check_encoding(filename, "utf-8", lines):
        return "utf-8"
    if check_encoding(filename, "gbk", lines):
        return "gbk"
    return ""


def guess_encoding(filename):
    return detect_encoding(filename, 100)
    

def usage():
    print("usage: FileDetect.py filename")
    
    
def main():
    if len(sys.argv) < 2:
        usage()
        return
    
    filename = sys.argv[1]
    if check_encoding(filename, "utf-8", 100):
        print('code.page=65001')
    return


if __name__ == "__main__":
    main()